package com.zcwin.runner.utils;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
*
*
* @author 王士江
* @version 1.0
*/
public class RedisManager {
    private static Logger logger   = Logger.getLogger(RedisManager.class);
    private static JedisPool jedisPool = null;
    
    public RedisManager(RedisConfig redisConfig)
    {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMinIdle(8);//
            config.setMaxTotal(redisConfig.getRedisMax_total());
            config.setMaxIdle(redisConfig.getRedisMax_idle());
            config.setMaxWaitMillis(redisConfig.getRedisMax_wait());
            config.setTestOnBorrow(redisConfig.isRedisTest_on_borrow());
            config.setTestOnReturn(true);
            //Idle
            config.setTestWhileIdle(true);
            //
            config.setTimeBetweenEvictionRunsMillis(30000);
            //
            config.setNumTestsPerEvictionRun(10);
            //
            config.setMinEvictableIdleTimeMillis(60000);

            jedisPool = new JedisPool(config, redisConfig.getRedisServer(), redisConfig.getRedisPort(), redisConfig.getRedisTimeout());
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 获取Jedis对象
     * @return
     */
    public synchronized static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                Jedis jedis = jedisPool.getResource();
                return jedis;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 释放jedis对象
     * @param jedis
     */
    private void returnResource(final Jedis jedis) {
        if (jedis != null) {
            jedisPool.returnResource(jedis);
        }
    }
    
    public boolean add(String key, Object value) {
    	Jedis jedis = null;  
    	try
    	{  
    		jedis = jedisPool.getResource();
    		jedis.set(key, (String)value);
    	}catch(Exception e) {  
    		e.printStackTrace();  
    		return false;
    	}finally{  
    		returnResource(jedis);  
    	} 
        return true;
    }
    
    public boolean addList(String key, String value) {
    	Jedis jedis = null;  
    	try
    	{  
    		jedis = jedisPool.getResource();
    		jedis.lpush(key, value);
    	}catch(Exception e) {  
    		e.printStackTrace();  
    		return false;
    	}finally{  
    		returnResource(jedis);  
    	} 
    	return true;
    }

    public boolean delete(String key) {
    	Jedis jedis = null;  
    	try
    	{  
    		jedis = jedisPool.getResource();
    		jedis.del(key);
    	}catch(Exception e) {  
    		e.printStackTrace();  
    		return false;
    	}finally{  
    		returnResource(jedis);  
    	} 
        return true;
    }

    public Object get(String key) {
		Jedis jedis = null;  
    	try
    	{  
    		jedis = jedisPool.getResource();
    		if(jedis.exists(key))
    		{
    			return jedis.get(key);
    		}
    	}catch(Exception e) {  
    		e.printStackTrace();  
    		return false;
    	}finally{  
    		returnResource(jedis);  
    	} 
    	//System.out.println("keyNotExists="+key);
    	return null;
    }
    
    public String getList(String key) {
    	Jedis jedis = null;  
    	try
    	{  
    		jedis = jedisPool.getResource();
    		if(jedis.exists(key))
    		{
    			return jedis.lpop(key);
    		}
    	}catch(Exception e) {  
    		e.printStackTrace();  
    	}finally{  
    		returnResource(jedis);  
    	} 
    	//System.out.println("keyNotExists="+key);
    	return null;
    }

    
    public void clear()
    {
    	Jedis jedis = null;  
    	try
    	{  
    		jedis = jedisPool.getResource();
    		jedis.flushAll();
    		jedis.flushDB();
    	}catch(Exception e) {  
    		e.printStackTrace();  
    	}finally{  
    		returnResource(jedis);  
    	} 
    }
}
